<?php

class Item
{    
    var $ID;
    var $IDNumOnly;
    var $supplier =  '';
    var $supplierName = '';
    var $desc = '';
    var $price;
    var $onhand;
	
    public function retrieve() 
    {
	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT tblitem.ID, tblitem.Description, 
		tblitem.Price, tblitem.OnHand, tblsupplier.Supplier, 
		tblsupplier.Code FROM tblitem INNER JOIN tblsupplier WHERE
		tblsupplier.Code= tblitem.supplier ");
	return $result;        
    }
    
    public function retrieveSuppliers() 
    {
	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT * from tblsupplier");
	return $result;        
    }
    
     public function retrieveByDesc($searchVal) 
    {
	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT tblitem.ID, tblitem.Description, 
		tblitem.Price, tblitem.OnHand, tblsupplier.Supplier, 
		tblsupplier.Code FROM tblitem INNER JOIN tblsupplier WHERE
		tblsupplier.Code = tblitem.supplier AND
		tblitem.Description LIKE '%$searchVal%'");
	return $result;        
    }
    
    public function retrieveByID($ItemCode) 
    {
	require('../includes/DBConn.inc');
	$result = mysql_query("SELECT tblitem.ID, tblitem.Description, 
		tblitem.Price, tblitem.OnHand, tblsupplier.Supplier, 
		tblsupplier.Code FROM tblitem INNER JOIN tblsupplier WHERE
		tblsupplier.Code = tblitem.supplier AND tblitem.ID='$ItemCode'");
	$row = mysql_fetch_array($result);
	$array = explode('-',$row['ID']);
	$this->ID = $row['ID'];
	$this->supplier =  $array[0];
	$this->IDNumOnly = $array[1];
	$this->supplierName =  $row['Supplier'];
        $this->desc = $row['Description'];
        $this->price = $row['Price'];
        $this->onhand = $row['OnHand'];
    }
    
    public function add()
    {       
      	 require('../includes/DBConn.inc');
      	 $query = "INSERT INTO tblitem (ID, Supplier, Description, Price, OnHand) 
      	           VALUES ('$_POST[code]-$_POST[id]','$_POST[code]', '$_POST[description]',
      	           $_POST[price], 0)";
      	      
      	 $result = mysql_query($query) or die("Error: ".mysql_error());
      	 return $result;
    }
    
    public function update($id)
    {
      	 require('../includes/DBConn.inc');
      	 $query = "UPDATE tblitem SET Description='$_POST[description]', 
      	 	  Price='$_POST[price]' WHERE ID='$id'";
      	 	   
      	 $result = mysql_query($query) or die("Error: ".mysql_error());
      	 return $result;
    }
    
    public function delete($id)
    {
    	 require('../includes/DBConn.inc');
         $result = mysql_query("DELETE FROM tblitem WHERE ID='$id'") or die(mysql_error());   
    }
    
}
?>